
shutter_channel=1;

if sel_roi>0
    tmp1=corrcoef(dx{z_plane},ROIs{z_plane}(sel_roi).activity);
    tmp2=corrcoef(dy{z_plane},ROIs{z_plane}(sel_roi).activity);
else
    tmp1=corrcoef(dx{z_plane},np.activity);
    tmp2=corrcoef(dy{z_plane},np.activity);
end
hccc=max(abs(tmp1(2)),abs(tmp2(2)));

if ~exist('h_adp_activity','var') || ~ishandle(h_adp_activity)
    aux_data(abs(aux_data)>1e10)=0;
    
    
    aux_data_plot=(downsample(aux_data',10)');
    figure(1003)
    clf
    mon_pos=get(0,'MonitorPositions');
    set(gcf,'color','k','menubar','none','position',[-1900 600 4500 400]);
    set(gcf,'keypressfcn','plot_aux_data_plot_kpf');
    axes('position',[0 0 1 1])
    hold on
    
    drawnow
    if exist('imeta_data','var')
        [imeta_data]=filter_eye_monitor_data(imeta_data);
    end
    
   
    [velP,velM,velP_ind,velM_ind]=get_vel_ind_from_adata(aux_data_plot);
    
    %plot_id=[1 2 6 9 3];
    plot_id=[3 9 4 5 6];
    
    colorind='rggyb';

 
    area(20*(velM_ind&~velP_ind),'linestyle','none','facecolor',[1 1 0]*0.25);
    area(20*(velP_ind&~velM_ind),'linestyle','none','facecolor',[0 1 0]*0.15);
    area(20*(velP_ind&velM_ind),'linestyle','none','facecolor',[1 1 1]*0.1);
    
    %plot laser power
    min_laser=median(aux_data_plot(5,:))-std(aux_data_plot(5,:));% min(aux_data_plot(5,round(size(aux_data_plot,2)/10):round(9*size(aux_data_plot,2)/10)));
    max_laser=median(aux_data_plot(5,:))+std(aux_data_plot(5,:));% max(aux_data_plot(5,round(size(aux_data_plot,2)/10):round(9*size(aux_data_plot,2)/10)));
    plot((aux_data_plot(5,:)-min_laser)/(max_laser-min_laser)+1.1*(length(plot_id)+2),'color',[1 1 1]*0.2);
    
    
    %plot air puff and noise tags
    if size(aux_data_plot,1)>7
        plot((aux_data_plot(9,:))/5+0.1,'color',[0 0 0.5]);
        plot((aux_data_plot(8,:))/5+0.1);
    end
    
    for aux_plot_ind=1:length(plot_id)
        plot((aux_data_plot(plot_id(aux_plot_ind),:)-min(aux_data_plot(plot_id(aux_plot_ind),:)))/(max(aux_data_plot(plot_id(aux_plot_ind),:))-min(aux_data_plot(plot_id(aux_plot_ind),:)))+1.1*aux_plot_ind-1,'color',colorind(aux_plot_ind));
    end
    
    vel_trace=downsample(abs(smooth2(velM,1000)),10);
    vel_trace=abs(smooth2(velM,1000));
    plot((vel_trace-min(vel_trace))/(max(vel_trace)-min(vel_trace))+4.5,'g');
    
    min_vel=prctile([velP velM],0.01);
    max_vel=prctile([velP velM],0.99);
    
    if sel_roi>0
        h_adp_activity=plot(frame_times(1:4:end)/10,2*(ROIs{z_plane}(sel_roi).activity-min(ROIs{z_plane}(sel_roi).activity))/(max(ROIs{z_plane}(sel_roi).activity)-min(ROIs{z_plane}(sel_roi).activity))+1.1*aux_plot_ind,'w');
    else
        h_adp_activity=plot(frame_times(1:4:end)/10,2*(np.activity-min(np.activity))/(max(np.activity)-min(np.activity))+1.1*aux_plot_ind,'w');
    end
    
    %plot registration correction
    plot(frame_times(1:4:end)/10,(dx{z_plane}-min(dx{z_plane}))/(max(dx{z_plane})-min(dx{z_plane}))+1.1*(aux_plot_ind+2),'b');
    plot(frame_times(1:4:end)/10,(dy{z_plane}-min(dy{z_plane}))/(max(dy{z_plane})-min(dy{z_plane}))+1.1*(aux_plot_ind+2),'r');
    
%     %plot aud data
%     if exist('aud_data','var')
%         comp_aud=compress_aud_data(aud_data,aux_data_plot(shutter_channel,:));
%         comp_aud=downsample(comp_aud,10);
%         plot((comp_aud-min(comp_aud))/(max(comp_aud)-min(comp_aud))+1.1*(aux_plot_ind+6));
%     end
    
    esf=0.8;
    if exist('imeta_data','var')
        nan_eye_ind=~isnan(imeta_data(7,:))&~isnan(imeta_data(8,:));
        if min(min(corrcoef(imeta_data(7,nan_eye_ind),imeta_data(8,nan_eye_ind))))>0
            eye_pos=imeta_data(7,:)+imeta_data(8,:);
        else
            eye_pos=imeta_data(7,:)-imeta_data(8,:);
        end
        plot(iframe_times(1:4:end)/10,(eye_pos-esf*min(eye_pos))/(esf*max(eye_pos)-esf*min(eye_pos))+1.1*(aux_plot_ind+3),'m');
        plot(iframe_times(1:4:end)/10,(imeta_data(9,:)-esf*min(imeta_data(9,:)))/(esf*max(imeta_data(9,:))-esf*min(imeta_data(9,:)))+1.1*(aux_plot_ind+4),'c');
        
        % plot whisker tracker data
        if std(imeta_data(3,:))>0.1
            wskx=imeta_data(3,:);
            wsky=imeta_data(4,:);
            wskx(wskx==0)=NaN;
            wsky(wsky==0)=NaN;
            wsk=diff(sqrt((wskx-nanmedian(wskx)).^2+(wsky-nanmedian(wsky)).^2));
            wsk=[wsk(1) wsk];
            %wsks=smooth(abs(wsk),10);
            plot(iframe_times(1:4:end)/10,(wsk-esf*min(wsk))/(esf*max(wsk)-esf*min(wsk))+1.1*(aux_plot_ind+5),'color',[0.5 0 1]);
            %plot(iframe_times(1:4:end),(wsks-min(wsk))/(max(wsk)-min(wsk))+1.1*(aux_plot_ind+5),'color',[0.5 0 1]);
        end
    end
    
    
    
    %     plot((velM-min_vel)/(max_vel-min_vel)+1.1*(aux_plot_ind+5),'g');
    %     plot((velP-min_vel)/(max_vel-min_vel)+1.1*(aux_plot_ind+6),'y');
    h_aux_txt=text(0,0.95,[' Cell nr. ' num2str(sel_roi) ' - ExpID ' fnames{1}(4:7) ' ' ROIs{z_plane}(sel_roi).type ' - hccc: ' num2str(hccc)],'color','w','units','normalized','fontsize',15,'fontweight','bold');
    axis tight
    ylim([0 1.1*(aux_plot_ind+7)])
    axis off
    %zoom on
    
else
    if sel_roi>0
        set(h_adp_activity,'ydata',2*(ROIs{z_plane}(sel_roi).activity-min(ROIs{z_plane}(sel_roi).activity))/(max(ROIs{z_plane}(sel_roi).activity)-min(ROIs{z_plane}(sel_roi).activity))+1.1*aux_plot_ind);
        set(h_aux_txt,'string',[' Cell nr. ' num2str(sel_roi) ' - ExpID ' fnames{1}(4:7) ' ' ROIs{z_plane}(sel_roi).type ' - hccc: ' num2str(hccc)]);
    else
        set(h_adp_activity,'ydata',2*(np.activity-min(np.activity))/(max(np.activity)-min(np.activity))+1.1*aux_plot_ind);
        set(h_aux_txt,'string',[' Cell nr. ' 'np' ' - ExpID ' fnames{1}(4:7) ' '  ' - hccc: ' num2str(hccc)]);
    end
end























